package edu.towson.cosc.cosc455.kchung2.project1;

import java.util.ArrayList;

public class MySemanticAnalyzer {
	//ArrayList<String> htmlTags = new ArrayList<String>();
	static ArrayList<String> htmlFile = new ArrayList<String>();
	/**
	 * Converts markdown tags into html tags.
	 * @param tree
	 * @return htmlFile
	 */
	public static ArrayList<String> convert(ArrayList<String> tree){
		for(int i = 0; i < tree.size(); i++){
			if(tree.get(i).equalsIgnoreCase(Tag.START_DOC)){
				htmlFile.add("<html>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_DOC)){
				htmlFile.add("</html>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_HEAD)){
				htmlFile.add("<head>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_HEAD)){
				htmlFile.add("</head>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_TITLE)){
				htmlFile.add("<title>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_TITLE)){
				htmlFile.add("</title>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_PARAGRAPH)){
				htmlFile.add("<paragraph>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_PARAGRAPH)){
				htmlFile.add("</paragraph>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_BOLD)){
				htmlFile.add("<bold>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_BOLD)){
				htmlFile.add("</bold>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_ITALICS)){
				htmlFile.add("<italics>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_ITALICS)){
				htmlFile.add("</italics>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_LIST)){
				htmlFile.add("<ul>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_LIST)){
				htmlFile.add("</ul>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_ITEM)){
				htmlFile.add("<li>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_ITEM)){
				htmlFile.add("</li>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.NEWLINE)){
				htmlFile.add("<br />");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_AUDIO)){
				htmlFile.add("<audio control>");
				i++;
				htmlFile.add("<source src = " + tree.get(i) +">");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_AUDIO)){
				htmlFile.add("</audio>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_VIDEO)){
				htmlFile.add("<iframe = ");
				i++;
				htmlFile.add(tree.get(i));
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_VIDEO)){
				htmlFile.add("/>");
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.START_USE)){
				i++;
				useVar(i,tree);
			}
			else if(tree.get(i).equalsIgnoreCase(Tag.END_USE)){
				i++;
			}
			else{
				htmlFile.add(tree.get(i));
			}
			
		}
		
		return htmlFile;
	}
	/**
	 * Checks to see if the variable exists
	 * @param t
	 */
	public static void useVar(int index, ArrayList<String> t){
		for(int j = 0; j < Compiler.variables.size(); j++){
			if(t.get(index).equals(Compiler.variables.get(j))){
				htmlFile.add(Compiler.values.get(j));
				return;
			}
		}
		System.out.println("Semantic Error: Variable not found.");
		System.exit(0);
	}
}
